package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.News;
import com.lczc.soa.domain.NewsExample;

import com.lczc.soa.domain.SearchNewsResponse;
import com.lczc.soa.mapper.NewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {


    @Autowired
    private NewsMapper newsMapper;



    @Override
    public News findNewsByTopic(String newsTopic) {
        return newsMapper.selectByTopic(newsTopic);
    }

    @Override
    public News findNewsByPrimaryKey(String id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public SearchNewsResponse findAllNews(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> newss = newsMapper.selectByExample(new NewsExample());
        return new SearchNewsResponse(((Page)newss).getTotal(),newss);
    }

    @Override
    public SearchNewsResponse findNewsLikeTopic(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> newss =newsMapper.selectByLikeTopic(keywords);
        return new SearchNewsResponse(((Page)newss).getTotal(),newss);
    }

    @Override
    public int saveNews(News news) {
        System.out.println("Service:"+news.getNewsTopic());
//        if(StringUtils.isNotBlank(news.getNewsId())){
//            return newsMapper.updateByPrimaryKey(news);
//
//        }else {
            return newsMapper.insert(news);

    }

    @Override
    public int deleteNews(String id) {
        return newsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public News findNewsById(String id) {
        return newsMapper.selectByPrimaryKey(id);

    }

    @Override
    public List <News> listNews() {
        return newsMapper.selectByExample(new NewsExample());
    }


}
